// pages/common/getauth/getauth.js
const app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        showLogin: false,
        buttonDisable: false,
        submitBtnDis: false,
        mobile: '',
        smsCode: '',
        codeButtonTxt: '获取短信验证码',
    },

    /**
     * 组件的方法列表
     */
    methods: {
        hideLogin() {
            this.setData({
                showLogin: false
            })
        },
        showLogin() {
            this.setData({
                showLogin: true
            })
        },
        //设置手机号
        setMobile(e) {
            var that = this
            that.setData({
                mobile: e.detail.value
            })
        },
        //设置验证码
        setSmsCode: function (e) {
            this.setData({
                smsCode: e.detail.value
            })
        },
        //获取短信验证码
        getCode: function () {
            var that = this
            if (that.data.mobile.length != 11) {
                wx.showModal({
                    title: '提示',
                    content: '请输入正确手机号',
                    showCancel: false
                })
                return false
            }
            wx.request({
                url: app.globalData.url + 'index/sendSmsCode2',
                data: {
                    mobile: that.data.mobile
                },
                success: function (msg) {
                    wx.showToast({
                        title: '发送成功',
                        icon: 'success',
                        duration: 1000
                    })
                    that.setData({
                        buttonDisable: true
                    })
                    that.timer(60)
                }
            })
        },
        //倒计时
        timer: function (time) {
            var that = this
            if (time == 0) {
                that.setData({
                    buttonDisable: null,
                    codeButtonTxt: '获取短信验证码'
                })
            } else {
                that.setData({
                    buttonDisable: true,
                    codeButtonTxt: time + '后重新获取'
                })
                time--
                setTimeout(function () {
                    that.timer(time)
                }, 1000)
            }
        },
        ourLogin() {
            let that = this
            let mobile = that.data.mobile
            let sms_code = that.data.smsCode
            if (mobile.length != 11) {
                wx.showModal({
                    title: '提示',
                    content: '请输入正确手机号',
                    showCancel: false
                })
                return false
            }
            if (sms_code.length != 6) {
                wx.showModal({
                    title: '提示',
                    content: '请输入正确的短信验证码',
                    showCancel: false
                })
                return false
            }
            let data = {
                'jscode': app.globalData.jscode,
                'mobile': mobile,
                'sms_code': sms_code,
                'type': 5,
                'encryptedData': app.globalData.encryptedData,
                'iv': app.globalData.iv
            }
            app.ajax({
                data: data,
                url: 'index/videoLikeMiniLogin',
                method: 'POST'
            }).then(response => {
                if (response.data.code == 200) {
                    that.loginCallback(response.data.data)
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        content: response.data.message,
                        showCancel: false
                    })
                }
            })
        },
        //获取手机号
        getPhoneNumber (e) {
            let that = this
            if (e.detail.encryptedData) {
                let data = {
                    'jscode': app.globalData.jscode,
                    'type': 5,
                    'encryptedData': app.globalData.encryptedData,
                    'iv': app.globalData.iv,
                    'authEncryptedData': e.detail.encryptedData,
                    'authIv': e.detail.iv
                }
                app.ajax({
                    data: data,
                    url: 'index/videoLikeMiniAuthLogin',
                    method: 'POST'
                }).then(response => {
                    if (response.data.code == 200) {
                        that.loginCallback(response.data.data)
                    } else {
                        wx.showModal({
                            title: '温馨提示',
                            content: response.data.message,
                            showCancel: false
                        })
                    }
                })
            }
        },
        //登录成功回调
        loginCallback(data){
            var sign = encodeURIComponent(data.sign)
            wx.setStorage({
                key: 'sign',
                data: sign
            })
            wx.setStorage({
                key: 'mobile',
                data: data.mobile
            })
            this.setData({
                showLogin: false
            })
            this.triggerEvent('loginSucess')
        }
    }
})